package qst.com.bacK.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import qst.com.bacK.exception.ExamException;
import qst.com.bacK.mapper.TestPersonnelMapper;
import qst.com.bacK.mapper.UserMapper;
import qst.com.bacK.pojo.entity.TestPersonnel;
import qst.com.bacK.pojo.entity.User;
import qst.com.bacK.service.TestPersonnelService;
import qst.com.bacK.utils.DateUtil;

import java.sql.SQLException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Author:   msh
 * Date:     2024/10/9 下午8:59
 * Description:
 */

@Service
public class TestPersonnelServiceImpl implements TestPersonnelService {
    @Autowired
    private TestPersonnelMapper testPersonnelMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public TestPersonnel findById(Integer id) {
        TestPersonnel testPersonnel = testPersonnelMapper.findById(id);
        User user = userMapper.findById(testPersonnel.getId());
        testPersonnel.setUser(user);
        return testPersonnel;
    }

    @Override
    public void addTestPersonnel(TestPersonnel s) {
        try {
            User u = s.getUser();
            if (u.getType() == 0) {
                u.setType(4);
            }

            u.setStatus(1);
            if (u.getPasswd() == null || u.getPasswd().isEmpty()) {
                u.setPasswd(User.PASSWORD);
            }

            u.setLogin(s.getPhone());// 学号作为默认登录名
            userMapper.insert(u);
            s.setId(u.getId());
            testPersonnelMapper.insert(s);
        } catch (SQLException ex) {
            throw new ExamException("添加参测人员出错", ex);
        }
    }

    @Override
    public TestPersonnel deleteTestPersonnel(int id) {
        try {
            User user = userMapper.findById(id);
            if (user.getLastLogin() != null) {
                throw new ExamException("该账户已启用，不能删除");
            }
            TestPersonnel testPersonnel = testPersonnelMapper.findById(id);
            testPersonnelMapper.delete(id);
            userMapper.delete(id);

            testPersonnel.setUser(user);
            return testPersonnel;
        } catch (SQLException e) {
            throw new ExamException("删除用户出错", e);
        }
    }

    @Override
    public void importTestPersonnel(List<TestPersonnel> testPersonnelList) {
        try {
            for (int i = 0; i < testPersonnelList.size(); i++) {
                TestPersonnel s = testPersonnelList.get(i);
                TestPersonnel temp = testPersonnelMapper.findByPhone(s.getPhone());
                if (temp == null)// 添加学生
                {
                    addTestPersonnel(s);
                } else // 更新已存在学生
                {
                    if (temp.getTeamId() != s.getTeamId()) {
                        throw new ExamException("导入第" + (i + 1) + "个参测人员出错,该手机号"
                                + s.getPhone() + "已在其他参测人员注册");
                    }
                    s.setId(temp.getId());
                    updateTestPersonnel(s);

                }
            }
        } catch (SQLException ex) {
            throw new ExamException("导入参测人员出错", ex);
        }
    }

    @Override
    public void updateTestPersonnel(TestPersonnel s) {
        try {
            User user = s.getUser();
            User u = userMapper.findById(s.getId());
            u.setName(user.getName());
            if (user.getStatus() != 0) {
                u.setStatus(user.getStatus());
            }
            userMapper.update(u);
            testPersonnelMapper.update(s);
        } catch (SQLException ex) {
            throw new ExamException("更新参测人员出错", ex);
        }
    }

    @Override
    public List<TestPersonnel> findByTeamId(int tid) {
        try {
            List<TestPersonnel> testPersonnels = testPersonnelMapper.findByTeamId(tid);
            return testPersonnels.stream().map(t -> t.setUser(userMapper.findById(t.getId())))
                    .collect(Collectors.toList());
        } catch (SQLException ex) {
            throw new ExamException("数据库查询出错");
        }
    }

    @Override
    public List<TestPersonnel> query(int teamId, String name, String phone) {
        try {
            List<TestPersonnel> testPersonnels = testPersonnelMapper.query(teamId, phone);
            if (name != null && !name.isEmpty()) {
                List<User> users = userMapper.findUserByLikeName(name);
                testPersonnels = testPersonnels.stream()
                        .filter(t -> users.stream().anyMatch(u -> u.getId().equals(t.getId())))
                        .collect(Collectors.toList());
            }
            return testPersonnels.stream().map(t -> t.setUser(userMapper.findById(t.getId())))
                    .collect(Collectors.toList());
        } catch (SQLException ex) {
            throw new ExamException("不存在该参测人员");
        }
    }
}
